package de.openlaboratory.xmleditor.servlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.xml.sax.SAXException;

/**
 * Servlet implementation class EditorServlet
 */
@WebServlet(
		description = "Main XML file editor servlet",
		urlPatterns = { "/xml" },
		initParams = { @WebInitParam(name = "xslFile", value = "none") })

public class OutputXmlFile extends HttpServlet
{
	private static final long serialVersionUID = 6647595593598178940L;
	
	private static final String XML_FILE = "D:\\DEV\\openlaboratory\\Open Laboratory XML Editor\\web\\res\\xml\\firma.xml";
	private static final String XSL_FILE = "D:\\DEV\\openlaboratory\\Open Laboratory XML Editor\\web\\res\\xml\\firma.xsl";

	/**
	 * Default constructor.
	 */
	public OutputXmlFile()
	{
		// TODO Auto-generated constructor stub
	}

	private void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException, TransformerException, ParserConfigurationException, SAXException
	{
		InputStream is;
		StreamSource srcXmlFile;
		StreamSource stylesheet;

		TransformerFactory tf = TransformerFactory.newInstance();

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		String transletName = request.getParameter("class");
		String documentURI = request.getParameter("source");

		if ((transletName == null) || (documentURI == null))
		{
			is = new FileInputStream(XSL_FILE);
			stylesheet = new StreamSource(is);

			is = new FileInputStream(XML_FILE);
			srcXmlFile = new StreamSource(is);

			// out.println("<h1>XSL transformation error</h1>");
			// out.println("The parameters <b><tt>class</tt></b> and " +
			// "<b><tt>source</tt></b> must be specified");
		}
		else
		{
			try
			{
				tf.setAttribute("use-classpath", Boolean.TRUE);
			}
			catch (IllegalArgumentException iae)
			{
				System.err.println("Could not set XSLTC-specific TransformerFactory attributes.");
			}

			stylesheet = new StreamSource(transletName);
			srcXmlFile = new StreamSource(documentURI);

		}

		try
		{
			Transformer t = tf.newTransformer(stylesheet);

			// Start the transformation
			final long start = System.currentTimeMillis();

			t.transform(srcXmlFile, new StreamResult(out));

			final long done = System.currentTimeMillis() - start;
			out.println("<!-- transformed by XSLTC in " + done + " msecs -->");
		}
		catch (Exception e)
		{
			out.println("<h1>Error</h1>");
			out.println(e.toString());
		}

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		try
		{
			doAction(request, response);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException
	{
		try
		{
			doAction(request, response);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
